<html>
<head><meta charset="utf-8"><title>Do types have a size? · t-lang · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/index.html">t-lang</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Do.20types.20have.20a.20size.3F.html">Do types have a size?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="218119794"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Do%20types%20have%20a%20size%3F/near/218119794" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Do.20types.20have.20a.20size.3F.html#218119794">(Nov 27 2020 at 18:43)</a>:</h4>
<p>(Continuing this discussion with <span class="user-mention" data-user-id="216206">@lcnr</span> in a new thread to avoid cluttering the old one)</p>



<a name="218119830"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Do%20types%20have%20a%20size%3F/near/218119830" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Do.20types.20have.20a.20size.3F.html#218119830">(Nov 27 2020 at 18:43)</a>:</h4>
<p><span class="user-mention silent" data-user-id="216206">lcnr</span> <a href="#narrow/stream/213817-t-lang/topic/mem.3A.3Asize_of.3A.20do.20not.20error.20if.20layout.20computations.20fail/near/217685843">said</a>:</p>
<blockquote>
<p>it's a used one though :_</p>
</blockquote>
<p>I'd say that is evidence of it being helpful. :D</p>



<a name="218119947"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Do%20types%20have%20a%20size%3F/near/218119947" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Do.20types.20have.20a.20size.3F.html#218119947">(Nov 27 2020 at 18:45)</a>:</h4>
<p><span class="user-mention silent" data-user-id="216206">lcnr</span> <a href="#narrow/stream/213817-t-lang/topic/mem.3A.3Asize_of.3A.20do.20not.20error.20if.20layout.20computations.20fail/near/217685768">said</a>:</p>
<blockquote>
<p>yeah, and i think defining <code>n</code> such that <code>\forall x: T, size_of_val(&amp;x) == n</code> to be the "size" of <code>T</code> is not a helpful notion <span aria-label="laughing" class="emoji emoji-1f606" role="img" title="laughing">:laughing:</span></p>
</blockquote>
<p>so, then, how do you even define the constraints that are inherently put on the layout of types such as <code>struct</code>, where the fields must be spaced to avoid overlaps? I think this all <em>cries</em> for a notion of "size of a type", and I am clearly not alone in thinking so as this is something that AFAIK pretty much every single language has.^^</p>



<a name="218121597"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Do%20types%20have%20a%20size%3F/near/218121597" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Do.20types.20have.20a.20size.3F.html#218121597">(Nov 27 2020 at 19:09)</a>:</h4>
<p>the size of values of type <code>T</code></p>



<a name="218121657"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Do%20types%20have%20a%20size%3F/near/218121657" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Do.20types.20have.20a.20size.3F.html#218121657">(Nov 27 2020 at 19:10)</a>:</h4>
<p>i currently think that always using "size of values" instead of plain "size" is preferable</p>



<a name="218121818"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Do%20types%20have%20a%20size%3F/near/218121818" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Do.20types.20have.20a.20size.3F.html#218121818">(Nov 27 2020 at 19:12)</a>:</h4>
<p>the reasoning behind this is fairly shallow though <span aria-label="sweat smile" class="emoji emoji-1f605" role="img" title="sweat smile">:sweat_smile:</span></p>



<a name="218121988"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Do%20types%20have%20a%20size%3F/near/218121988" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Do.20types.20have.20a.20size.3F.html#218121988">(Nov 27 2020 at 19:14)</a>:</h4>
<p>i think that "size" of <code>T</code> quite strongly suggests that if <code>T</code> is <code>Sized</code>, <code>T</code> must have a valid size to be well formed</p>



<a name="218121993"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Do%20types%20have%20a%20size%3F/near/218121993" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Do.20types.20have.20a.20size.3F.html#218121993">(Nov 27 2020 at 19:14)</a>:</h4>
<p>which is not the case</p>



<a name="218122440"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Do%20types%20have%20a%20size%3F/near/218122440" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Do.20types.20have.20a.20size.3F.html#218122440">(Nov 27 2020 at 19:20)</a>:</h4>
<blockquote>
<p>I'd say that is evidence of it being helpful. :D</p>
</blockquote>
<p>only because something is used doesn't mean it's helpful <span aria-label="sparkles" class="emoji emoji-2728" role="img" title="sparkles">:sparkles:</span></p>



<a name="218122991"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Do%20types%20have%20a%20size%3F/near/218122991" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Do.20types.20have.20a.20size.3F.html#218122991">(Nov 27 2020 at 19:28)</a>:</h4>
<p>or maybe "size" is actually fine</p>



<a name="218123156"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Do%20types%20have%20a%20size%3F/near/218123156" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Do.20types.20have.20a.20size.3F.html#218123156">(Nov 27 2020 at 19:31)</a>:</h4>
<p>the main reason why I wanted this change is that with types not having a size, I wouldn't have needed as long to figure out a reasoning for why we don't need wf bounds for <code>[u16; N]</code></p>



<a name="218123243"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Do%20types%20have%20a%20size%3F/near/218123243" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Do.20types.20have.20a.20size.3F.html#218123243">(Nov 27 2020 at 19:32)</a>:</h4>
<p>now that <span class="user-mention silent" data-user-id="124288">oli</span> and I already spend the time thinking about this we hopefully won't have to do so again</p>



<a name="218123257"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Do%20types%20have%20a%20size%3F/near/218123257" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Do.20types.20have.20a.20size.3F.html#218123257">(Nov 27 2020 at 19:32)</a>:</h4>
<p>so it doesn't really matter too much anymore</p>



<a name="218125531"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Do%20types%20have%20a%20size%3F/near/218125531" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Do.20types.20have.20a.20size.3F.html#218125531">(Nov 27 2020 at 20:06)</a>:</h4>
<p>Is it potentially relevant that we may in the future have types for which the values are not all the same size, but for which the values do <em>have</em> a size? For instance, variable-length structures?</p>



<a name="218125757"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Do%20types%20have%20a%20size%3F/near/218125757" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Do.20types.20have.20a.20size.3F.html#218125757">(Nov 27 2020 at 20:08)</a>:</h4>
<p>these would have to be <code>!Sized</code> afaict</p>



<a name="218125943"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Do%20types%20have%20a%20size%3F/near/218125943" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Do.20types.20have.20a.20size.3F.html#218125943">(Nov 27 2020 at 20:11)</a>:</h4>
<p>Right.</p>



<a name="218126036"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Do%20types%20have%20a%20size%3F/near/218126036" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Do.20types.20have.20a.20size.3F.html#218126036">(Nov 27 2020 at 20:12)</a>:</h4>
<p>Which means the type doesn't have a size known at compile time. So at that point the <em>type</em> doesn't have a fixed size, but any particular value does.</p>



<a name="218157559"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Do%20types%20have%20a%20size%3F/near/218157559" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Do.20types.20have.20a.20size.3F.html#218157559">(Nov 28 2020 at 10:08)</a>:</h4>
<p><span class="user-mention silent" data-user-id="216206">lcnr</span> <a href="#narrow/stream/213817-t-lang/topic/Do.20types.20have.20a.20size.3F/near/218121597">said</a>:</p>
<blockquote>
<p>the size of values of type <code>T</code></p>
</blockquote>
<p>but then you need a condition that all values of type <code>T</code> have the same size</p>



<a name="218157563"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Do%20types%20have%20a%20size%3F/near/218157563" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Do.20types.20have.20a.20size.3F.html#218157563">(Nov 28 2020 at 10:08)</a>:</h4>
<p>so there is still a specifically called-out size somewhere, which we might as well call the size of the type</p>



<a name="218157580"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Do%20types%20have%20a%20size%3F/near/218157580" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Do.20types.20have.20a.20size.3F.html#218157580">(Nov 28 2020 at 10:10)</a>:</h4>
<p><span class="user-mention silent" data-user-id="216206">lcnr</span> <a href="#narrow/stream/213817-t-lang/topic/Do.20types.20have.20a.20size.3F/near/218122440">said</a>:</p>
<blockquote>
<blockquote>
<p>I'd say that is evidence of it being helpful. :D</p>
</blockquote>
<p>only because something is used doesn't mean it's helpful <span aria-label="sparkles" class="emoji emoji-2728" role="img" title="sparkles">:sparkles:</span></p>
</blockquote>
<p>it's not <em>proof</em>, but it's <em>evidence</em>. people are less likely to use it if it isn't helpful.<br>
FWIW, I find it a very helpful notion, in fact I find it essential and I claim that it will always be present (in the condition that all values of the type have the same size) even if you refuse to name it.</p>



<a name="218157639"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Do%20types%20have%20a%20size%3F/near/218157639" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Do.20types.20have.20a.20size.3F.html#218157639">(Nov 28 2020 at 10:11)</a>:</h4>
<p><span class="user-mention silent" data-user-id="239881">Josh Triplett</span> <a href="#narrow/stream/213817-t-lang/topic/Do.20types.20have.20a.20size.3F/near/218126036">said</a>:</p>
<blockquote>
<p>Which means the type doesn't have a size known at compile time. So at that point the <em>type</em> doesn't have a fixed size, but any particular value does.</p>
</blockquote>
<p>that is already the case for <code>!Sized</code> types now. every trait object and slice has an underlying true size returned by <code>size_of_val</code>.</p>



<a name="218196527"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Do%20types%20have%20a%20size%3F/near/218196527" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> isHavvy <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Do.20types.20have.20a.20size.3F.html#218196527">(Nov 29 2020 at 05:04)</a>:</h4>
<blockquote>
<p>Since all values of a Sized type share the same size and alignment, we refer to those shared values as the size of the type and the alignment of the type respectively.</p>
</blockquote>
<p>-- <a href="https://doc.rust-lang.org/stable/reference/type-layout.html">Rust Reference</a></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>